<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!--卡片区域列表-->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!--订单列表数据-->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="订单编号" prop="orderNumber"></el-table-column>
        <el-table-column label="订单价格" prop="orderPrice"></el-table-column>
        <el-table-column label="是否付款" prop="payStatus">
          <template v-slot="scope">
            <el-tag type="success" v-if="scope.row.payStatus == '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="isSend">
          <template v-slot="scope">
            {{scope.row.isSend}}
          </template>
        </el-table-column>
        <el-table-column label="下单时间" prop="createTime">
          <template v-slot="scope">
            {{scope.row.createTime | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEdit(scope.row.orderId)"></el-button>
            <el-button type="success" icon="el-icon-location" size="mini" @click="showProgress"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageIndex"
        :page-sizes="[10, 20, 30, 40,50]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!--修改地址对话框-->
    <el-dialog
      title="编辑地址"
      :visible.sync="editDialogVisible"
      width="50%" @close="editDialogClose">
      <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="addressForm.address1"
                       @change="getCityData" clearable ref="sysCascaderRef">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editButton">确 定</el-button>
  </span>
    </el-dialog>

    <!--展示物流进度地址对话框-->
    <el-dialog
      title="物流进度"
      :visible.sync="locationDialogVisible"
      width="50%">

      <!--时间线-->
      <el-timeline :reverse="true">
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.ftime">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
  import cityData from './city-data.json'
    export default {
        name: "Order",
      data(){
          return{
            queryInfo:{
              query:'',
              pageIndex:1,
              pageSize:10
            },
            orderList:[],
            total:0,
            editDialogVisible:false,
            addressForm:{
              address1:[],
              address2:''
            },
            addressFormRules:{
              address1:[{required:true,message:'请选择省市区县',trigger:'blur'}],
              address2:[{required:true,message:'请填写详细地址',trigger:'blur'}]
            },
            cityData,
            cityDataValue:[],
            cascaderProps:{
              expandTrigger: 'hover'
            },
            locationDialogVisible:false,
            progressInfo:[],
            orderId:'',
            address3:''
          }
      },
      created() {
          this.getOrderList();
      },
      methods:{
        async getOrderList(){
          const {data:res} = await this.$axios.get("orders",{params:this.queryInfo});
          if(res.code != 200){
            this.$message.error("获取订单列表失败");
          }
          this.$message.success("获取订单列表成功");
          this.orderList = res.data.orders;
          this.total = res.data.total;
          console.log(this.orderList);
        },
        handleSizeChange(val){
          this.queryInfo.pageSize = val;
          this.getOrderList();
        },
        handleCurrentChange(val){
          this.queryInfo.pageIndex = val;
          this.getOrderList();
        },
        showEdit(id){
          this.orderId = id;
          this.editDialogVisible = true;
        },
        editDialogClose(){
          this.orderId = '';
          this.$nextTick(()=>{
            this.$refs.addressFormRef.resetFields();
          })
        },
        async showProgress(){
          //Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1OTYwMDM2NTMsImV4cCI6MTU5NjA5MDA1M30.TXocEjuureq3y0AWixX5rFoT-ax4bx5tWrJEVZxsp5Y
          //{"meta":{"status":200,"message":"获取物流信息成功！"},"data":[{"time":"2020-07-29 08:26:29","ftime":"2020-07-29 08:26:29","context":"[苏州]位于当地的联邦快递递送站","location":"苏州"},{"time":"2020-07-29 04:39:34","ftime":"2020-07-29 04:39:34","context":"[上海]离开联邦快递工作地点","location":"上海"},{"time":"2020-07-28 22:37:17","ftime":"2020-07-28 22:37:17","context":"[上海]到达联邦快递工作地点","location":"上海"},{"time":"2020-07-28 20:37:05","ftime":"2020-07-28 20:37:05","context":"[上海]已离开发件地","location":"上海"},{"time":"2020-07-28 17:48:50","ftime":"2020-07-28 17:48:50","context":"[上海]已取件","location":"上海"}]}
          //{"data":null,"meta":{"msg":"无效token","status":400}}
          const {data:res} = await this.$axios.get("http://timemeetyou.com:8889/api/private/v1/kuaidi/804909574412544580",
            {headers: {'Authorization':'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1OTYwMDM5MjUsImV4cCI6MTU5NjA5MDMyNX0.80OKQz7YbQV0mOuZUH9jlja50IRUsQsOsx__p0pNo-k'}});

          if(res.meta.status != 200){
            this.$message.error("获取无酒进度失败");
          }
          this.progressInfo = res.data;
          this.locationDialogVisible = true;
          console.log(this.progressInfo);
        },
        getCityData(val){
          //console.log(val);
          const pathvalue = this.$refs.sysCascaderRef.getCheckedNodes()[0];
          if(pathvalue != ""){
            this.address3 = pathvalue.pathLabels.join(" ");
          }
          //console.log(pathvalue.label);console.log(pathvalue.pathLabels),

        },
        async editButton() {
          const {data:res} = await this.$axios.put("update/address/"+this.orderId,
            "address1="+this.address3+"&address2="+this.addressForm.address2);
          if(res.code != 200){
            this.$message.error("修改订单地址失败");
          }
          this.$message.success("修改订单地址成功");
          this.getOrderList();
          this.editDialogVisible = false;
        }
      },
      //自定义过滤器
      filters:{
        dateFormat(originVal){
          const dt = new Date(originVal);
          const y = dt.getFullYear();
          const m = (dt.getMonth() +1 + '').padStart(2,'0');
          const d = (dt.getDay() + '').padStart(2,'0');
          const hh = (dt.getHours() + '').padStart(2,'0');
          const mm = (dt.getMinutes() + '').padStart(2,'0');
          const ss = (dt.getSeconds() + '').padStart(2,'0');
          //return y;
          return y+'-'+m+'-'+d+' '+hh+':'+mm+':'+ss;
        }
      }
    }
</script>

<style scoped>
.el-cascader{
  width: 100%;
}
</style>
